<?php

namespace card\app;

use card\models\Card;
use card\models\CardBatch;
use framework\common\BasicController;
use Yii;
use yii\data\ActiveDataProvider;

class IndexController extends BasicController
{
    /**
     * 重写父类
     * @return [type] [description]
     */
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);
        unset($actions['view']);
        unset($actions['create']);
        unset($actions['update']);
        return $actions;
    }

    public function actionIndex()
    {
        //获取头部信息
        $headers = Yii::$app->getRequest()->getHeaders();
        //获取分页信息
        $pageSize = $headers->get('X-Pagination-Per-Page') ?? 20;
        $AppID = Yii::$app->params['AppID'];
        $where = ['UID' => \Yii::$app->user->id, 'AppID' => $AppID, 'is_deleted' => 0];
//        $where = ['and', $where, ['is_used' => 0], ['>', 'end_time', time()]];
        $orderBy = ['end_time' => SORT_DESC];
        $page_id = Yii::$app->request->get('page_id', null);
        if ($page_id){
            $where['page_id'] = $page_id;
        }

        $data = new ActiveDataProvider(
            [
                'query' => Card::find()
                    ->where($where)
                    ->orderBy($orderBy)
                    ->asArray(),
                'pagination' => ['pageSize' => $pageSize, 'validatePage' => false],
            ]
        );
        $list = $data->getModels();

        $list = str2url($list);
        $data->setModels($list);
        return $data;
    }

    public function actionView()
    {
        $id = Yii::$app->request->get('id', false);
        $where = ['id' => $id, 'UID' => \Yii::$app->user->id, 'is_deleted' => 0];
        $result = Card::find()
            ->where($where)
            ->asArray()
            ->one();
        if (empty($result)) {
            Error('卡密不存在');
        }
        $result = str2url($result);
        return $result;
    }

    /**
     * 绑定福利卡
     * @return [type] [description]
     */
    public function actionCreate()
    {
        $post = \Yii::$app->request->post();
        $card_number = $post['card_number'] ?? '';
        $card_password = $post['card_password'] ?? '';
        if ($card_number === '') {
            Error('卡号不能为空');
        }
        if ($card_password === '') {
            Error('卡密不能为空');
        }
        $where = ['number' => $card_number, 'is_deleted' => 0];
        $card = Card::find()
            ->where($where)
            ->one();
        if (empty($card) || $card['password'] !== strtoupper($card_password)) {
            Error('卡密不正确');
        }
        if ($card['is_bind'] == 1) {
            Error('卡密已被使用');
        }
        if ($card['status'] != 1) {
            Error('当前卡未处于入库状态，不能绑定');
        }
        // 绑定福利卡
        $card->is_bind = 1;
        $card->UID = Yii::$app->user->identity->id;
        $card->bind_time = time();
        $card->begin_time = $card->end_time>0?$card->created_time:time();
        $card->end_time = $card->end_time>0?$card->end_time:(time() + 60 * 60 * 24 * $card->expire_day);
        $card->save();
        $card_batch = CardBatch::find()->where(['id' => $card->card_batch_id])->one();
        if ($card_batch) {
            $active_num = Card::find()->where(['card_batch_id' => $card->card_batch_id, 'is_bind' => 1])->count();
            $card_batch->active_num = $active_num;
            $card_batch->save();
        }
        return $card;
    }
}
